R Data Frames Exercises - Solutions

For this exercise we will test your knowledge of data frames! Just follow the exercise instructions that are in bold below!

Ex 1: Recreate the following dataframe by creating vectors and using the data.frame function:

In [3]:
Name <- c("Sam","Frank","Amy")
Age <- c(22,25,26)
Weight <- c(150,165,120)
Sex <- c("M", "M", "F")
df <- data.frame (row.names = Name, Age, Weight, Sex)
df
Out[3]:
AgeWeightSex
Sam22150M
Frank25165M
Amy26120F

Ex 2: Check if mtcars is a dataframe using is.data.frame()

In [1]:
is.data.frame(mtcars)
Out[1]:
TRUE

Ex 3: Use as.data.frame() to convert a matrix into a dataframe:

In [3]:
mat <- matrix(1:25,nrow = 5)
as.data.frame(mat)
Out[3]:
V1V2V3V4V5
116111621
227121722
338131823
449141924
5510152025

Ex 4: Set the built-in data frame mtcars as a variable df. We'll use this df variable for the rest of the exercises.

In [4]:
df <- mtcars

Ex 5: Display the first 6 rows of df

In [5]:
head(df)
Out[5]:
mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX42161601103.92.6216.460144
Mazda RX4 Wag2161601103.92.87517.020144
Datsun 71022.84108933.852.3218.611141
Hornet 4 Drive21.462581103.083.21519.441031
Hornet Sportabout18.783601753.153.4417.020032
Valiant18.162251052.763.4620.221031

Ex 6: What is the average mpg value for all the cars?

In [6]:
mean(mtcars$mpg)
Out[6]:
20.090625

Ex 7: Select the rows where all cars have 6 cylinders (cyl column)

In [7]:
df[df$cyl==6,]
Out[7]:
mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX42161601103.92.6216.460144
Mazda RX4 Wag2161601103.92.87517.020144
Hornet 4 Drive21.462581103.083.21519.441031
Valiant18.162251052.763.4620.221031
Merc 28019.26167.61233.923.4418.31044
Merc 280C17.86167.61233.923.4418.91044
Ferrari Dino19.761451753.622.7715.50156

Ex 8: Select the columns am,gear, and carb.

In [8]:
df[,c('am','gear','carb')]
Out[8]:
amgearcarb
Mazda RX4144
Mazda RX4 Wag144
Datsun 710141
Hornet 4 Drive031
Hornet Sportabout032
Valiant031
Duster 360034
Merc 240D042
Merc 230042
Merc 280044
Merc 280C044
Merc 450SE033
Merc 450SL033
Merc 450SLC033
Cadillac Fleetwood034
Lincoln Continental034
Chrysler Imperial034
Fiat 128141
Honda Civic142
Toyota Corolla141
Toyota Corona031
Dodge Challenger032
AMC Javelin032
Camaro Z28034
Pontiac Firebird032
Fiat X1-9141
Porsche 914-2152
Lotus Europa152
Ford Pantera L154
Ferrari Dino156
Maserati Bora158
Volvo 142E142

Ex 9: Create a new column called performance, which is calculated by hp/wt.

In [13]:
df$performance <- df$hp/df$wt
In [14]:
head(df)
Out[14]:
mpgcyldisphpdratwtqsecvsamgearcarbperformance
Mazda RX42161601103.92.6216.46014441.98473
Mazda RX4 Wag2161601103.92.87517.02014438.26087
Datsun 71022.84108933.852.3218.61114140.08621
Hornet 4 Drive21.462581103.083.21519.44103134.21462
Hornet Sportabout18.783601753.153.4417.02003250.87209
Valiant18.162251052.763.4620.22103130.34682

Ex 10: Your performance column will have several decimal place precision. Figure out how to use round() (check help(round)) to reduce this accuracy to only 2 decimal places.

In [16]:
df$performance <- round(df$performance,2)
In [17]:
head(df)
Out[17]:
mpgcyldisphpdratwtqsecvsamgearcarbperformance
Mazda RX42161601103.92.6216.46014441.98
Mazda RX4 Wag2161601103.92.87517.02014438.26
Datsun 71022.84108933.852.3218.61114140.09
Hornet 4 Drive21.462581103.083.21519.44103134.21
Hornet Sportabout18.783601753.153.4417.02003250.87
Valiant18.162251052.763.4620.22103130.35

Ex 10: What is the average mpg for cars that have more than 100 hp AND a wt value of more than 2.5.

In [20]:
mean(subset(df,hp>100 & wt > 2.5)$mpg)
Out[20]:
16.8636363636364
In [22]:
# Lots of ways to do this
mean(df[df$hp>100 & df$wt > 2.5,]$mpg)
Out[22]:
16.8636363636364

Ex 11: What is the mpg of the Hornet Sportabout?

In [26]:
df['Hornet Sportabout',]$mpg
Out[26]:
18.7

Great Job!

You'll get even more practice on these operations during your final project!